<template>
  <el-dialog title="选择场景" :visible.sync="visible" width="800px" append-to-body>
    <div class="scene-select-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="80px">
        <el-form-item label="场景名称" prop="sceneName">
          <el-input
            v-model="queryParams.sceneName"
            placeholder="请输入场景名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table
        v-loading="loading"
        :data="sceneList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="场景名称" align="center" prop="sceneName" />
        <el-table-column label="状态" align="center" prop="isEnabled" width="100">
          <template slot-scope="scope">
            <el-tag :type="scope.row.isEnabled ? 'success' : 'info'">
              {{ scope.row.isEnabled ? '启用' : '停用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="触发条件" align="center" prop="matchMode" width="100">
          <template slot-scope="scope">
            {{ scope.row.matchMode === 1 ? '任意条件' : scope.row.matchMode === 2 ? '所有条件' : '不满足' }}
          </template>
        </el-table-column>
        <el-table-column label="执行方式" align="center" prop="execType" width="100">
          <template slot-scope="scope">
            {{ scope.row.execType === 1 ? '顺序执行' : '同时执行' }}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelection">确定</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listScene } from "@/api/iot/scene";

export default {
  name: "SceneSelect",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      selectedScenes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场景表格数据
      sceneList: [],
      // 是否显示弹出层
      visible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sceneName: null,
        status: null
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询场景列表 */
    getList() {
      this.loading = true;
      listScene(this.queryParams).then(response => {
        this.sceneList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.visible = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.selectedScenes = [];
      this.resetForm("queryForm");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.selectedScenes = selection;
    },
    // 显示弹窗
    show() {
      this.visible = true;
      this.getList();
    },
    // 确认选择
    confirmSelection() {
      this.$emit('selectScenes', this.selectedScenes);
      this.visible = false;
      this.reset();
    }
  }
};
</script>

<style scoped>
.scene-select-container {
  padding: 10px;
}
</style> 